import { FindOneOptions, MongoDistinctPreferences } from 'mongodb';

import { BaseRaw } from './BaseRaw';
import { ILivechatInquiryRecord, LivechatInquiryStatus } from '../../../../definition/IInquiry';

export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> {
	findOneQueuedByRoomId(rid: string): Promise<ILivechatInquiryRecord & { status: LivechatInquiryStatus.QUEUED } | null> {
		const query = {
			rid,
			status: LivechatInquiryStatus.QUEUED,
		};
		return this.findOne(query) as unknown as (Promise<(ILivechatInquiryRecord & { status: LivechatInquiryStatus.QUEUED }) | null>);
	}

	findOneByRoomId<T = ILivechatInquiryRecord>(rid: string, options: FindOneOptions<T extends ILivechatInquiryRecord ? ILivechatInquiryRecord: T>): Promise<T | null> {
		const query = {
			rid,
		};
		return this.findOne(query, options);
	}

	getDistinctQueuedDepartments(options: MongoDistinctPreferences): Promise<string[]> {
		return this.col.distinct('department', { status: LivechatInquiryStatus.QUEUED }, options);
	}
}
